#!/bin/bash
set -e

BUILD_DEBUG="${BUILD_DEBUG:-}"
CI_BUILD_TAG="${DRONE_TAG:-}"
CI_BUILD_TAG="${CI_BUILD_TAG#v}"

echo "CI_BUILD_TAG ${CI_BUILD_TAG}..."


if [[ -n "${BUILD_DEBUG}" ]]; then
  set -x
  env
fi

# cd to app root
CWD=$(dirname $0)
if [[ `basename $(pwd)` = 'scripts' ]]; then
  cd ../
else
  cd `dirname $CWD`
fi

echo "Cleaning Up.."
rm -rf node_modules build dist tmp

echo "Bootstrapping.."
./scripts/bootstrap

if [[ -z "${CI_BUILD_TAG}" ]]; then
  echo "Linting.."
  ./node_modules/.bin/eslint --max-warnings 0 .
fi

if [[ "${BUILD_LATEST}" == "true" ]]; then
  echo "Building latest..."
  ./scripts/build-static -l -v latest-2.5
  # (build-static contains a call to test & build)
elif [[ ! -z "${CI_BUILD_TAG}" ]]; then
  echo "Building v${CI_BUILD_TAG}..."
  ./scripts/build-static -s -v "${CI_BUILD_TAG}"
  # (build-static contains a call to test & build)
else
  echo "Testing..."
  yarn test
fi
